package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.CourseQueryDTO;
import com.jeff.tianti.cms.dto.TeachQueryDTO;
import com.jeff.tianti.cms.entity.Course;
import com.jeff.tianti.cms.entity.Teach;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ColumnInfoDaoImpl类
 * @date 2017-03-16
 */

public class CourseDaoImpl extends CustomBaseSqlDaoImpl implements CourseDaoCustom {

    public PageModel<Course> queryCoursePage(CourseQueryDTO courseQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Course t where 1=1 and t.deleteFlag = 0 ");
        if (courseQueryDTO != null) {

            if (StringUtils.isNotBlank(courseQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + courseQueryDTO.getName() + "%");
            }

            if (StringUtils.isNotBlank(courseQueryDTO.getColumnId())) {
                hql.append(" and t.columnId like :columnId ");
                map.put("columnId", courseQueryDTO.getColumnId());
            }

            if (StringUtils.isNotBlank(courseQueryDTO.getIsBest())) {
                hql.append(" and t.isBest like :isBest ");
                map.put("isBest", courseQueryDTO.getIsBest());
            }

            if (courseQueryDTO.getStartTime() != null) {
                hql.append(" and t.startTime > :startTime ");
                map.put("startTime", courseQueryDTO.getStartTime());
            }

            if (courseQueryDTO.getEndTime() != null) {
                hql.append(" and t.startTime < :endTime ");
                map.put("endTime", courseQueryDTO.getEndTime());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, courseQueryDTO.getCurrentPage(), courseQueryDTO.getPageSize());
    }

    public List<Course> queryCourseList(CourseQueryDTO courseQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Course t where 1=1 and t.deleteFlag = 0 ");
        if (courseQueryDTO != null) {
            if (StringUtils.isNotBlank(courseQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + courseQueryDTO.getName() + "%");
            }

            if (StringUtils.isNotBlank(courseQueryDTO.getTeachId())) {
                hql.append(" and t.teachId like :teachId ");
                map.put("teachId", courseQueryDTO.getTeachId());
            }

            if (StringUtils.isNotBlank(courseQueryDTO.getColumnId())) {
                hql.append(" and t.columnId = :columnId ");
                map.put("columnId", courseQueryDTO.getColumnId());
            }
        }
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public int updateSelectiveById(Course course) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update Course t set");
        if (course != null) {

            if (!StringUtils.isNotBlank(course.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(course.getName())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.name = :name ");
                } else {
                    hql.append(" t.name = :name ");
                }
                map.put("name", course.getName());
            }

            if (StringUtils.isNotBlank(course.getCoverImg())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.coverImg = :coverImg ");
                } else {
                    hql.append(" t.coverImg = :coverImg ");
                }
                map.put("coverImg", course.getCoverImg());
            }


            if (StringUtils.isNotBlank(course.getColumnId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.columnId = :columnId ");
                } else {
                    hql.append(" t.columnId = :columnId ");
                }
                map.put("columnId", course.getColumnId());
            }

            if (StringUtils.isNotBlank(course.getDescription())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.description = :description ");
                } else {
                    hql.append(" t.description = :description ");
                }
                map.put("description", course.getDescription());
            }

            if (StringUtils.isNotBlank(course.getApplyTo())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.applyTo = :applyTo ");
                } else {
                    hql.append(" t.applyTo = :applyTo ");
                }
                map.put("applyTo", course.getApplyTo());
            }
            if (course.getStartTime() != null) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.startTime = :startTime ");
                } else {
                    hql.append(" t.startTime = :startTime ");
                }
                map.put("startTime", course.getStartTime());
            }

            if (StringUtils.isNotBlank(course.getIsBest())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.isBest = :isBest ");
                } else {
                    hql.append(" t.isBest = :isBest ");
                }
                map.put("isBest", course.getIsBest());
            }

            if (StringUtils.isNotBlank(course.getDeleteFlag())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.deleteFlag = :deleteFlag ");
                } else {
                    hql.append(" t.deleteFlag = :deleteFlag ");
                }
                map.put("deleteFlag", course.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(course.getRootColumnInfoId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.rootColumnInfoId = :rootColumnInfoId ");
                } else {
                    hql.append(" t.rootColumnInfoId = :rootColumnInfoId ");
                }
                map.put("rootColumnInfoId", course.getRootColumnInfoId());
            }

            if (StringUtils.isNotBlank(course.getTeachId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.teachId = :teachId ");
                } else {
                    hql.append(" t.teachId = :teachId ");
                }
                map.put("teachId", course.getTeachId());
            }


            hql.append("where t.id=:id ");
            map.put("id", course.getId());
        }
        return this.update(hql.toString(), map);
    }

    public PageModel<Map<String, Object>> queryCourseList(PageBean pageBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT course.id,DATE_FORMAT( course.start_time, '%Y-%m-%d' ) AS startTime,course.name,course.count,course.cover_img AS coverImg ,cms_column_info.name as cateName ,course.root_column_info_id as columnId  FROM course LEFT JOIN cms_column_info on cms_column_info.id=course.column_id WHERE course.delete_flag = 0 ORDER BY start_time DESC ");
        String baseSql = "SELECT count(*) FROM course LEFT JOIN cms_column_info on cms_column_info.id=course.column_id WHERE course.delete_flag = 0 ";
        return this.querySqlObjects(hql.toString(), baseSql, map, pageBean.getCurrentPage() + 1, pageBean.getPageSize());
    }

    public PageModel<Map<String, Object>> queryCourseListByTeachId(String teachId, int currentPage, int totalPage) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,DATE_FORMAT(start_time,'%Y-%m-%d') as startTime ,name, count, description,cover_img as coverImg FROM course where delete_flag=0 and teach_id=:teachId order by start_time desc  ");
        String baseSql = "SELECT count(*) FROM course where delete_flag=0 and teach_id=:teachId ";
        map.put("teachId", teachId);

        return this.querySqlObjects(hql.toString(), baseSql, map, currentPage, totalPage);
    }

    public PageModel<Map<String, Object>> queryCourseListByBast(int currentPage, int totalPage) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,DATE_FORMAT(start_time,'%Y-%m-%d') as startTime ,name, count, description,cover_img as coverImg FROM course where delete_flag=0 and is_best=1  order by start_time desc  ");
        String baseSql = "SELECT count(*) FROM course where delete_flag=0 and is_best=1 ";
        return this.querySqlObjects(hql.toString(), baseSql, map, currentPage, totalPage);
    }

    public List<Map<String, Object>> getLastSixDayCourse(String startTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        String startDate =startTime+" 00:00:00";
        String endDate =startTime+" 23:59:59";
        hql.append(" SELECT id,DATE_FORMAT(start_time,'%Y-%m-%d') as startTime ,name, count,cover_img as coverImg FROM course where delete_flag=0  and start_time >:startDate and start_time <:endDate order by start_time desc  ");
        map.put("startDate", startDate);
        map.put("endDate", endDate);

        return this.querySqlObjects(hql.toString(), map,null,null);
    }

    public List<Map<String, Object>> getCourseListByCateId(String columnId) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,DATE_FORMAT(start_time,'%Y-%m-%d') as startTime ,name, description,count,cover_img as coverImg FROM course where delete_flag=0  and column_id =:columnId order by start_time desc  ");
        map.put("columnId", columnId);

        return this.querySqlObjects(hql.toString(), map,null,null);
    }

    public List getLastSixDay() {
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT DATE_FORMAT( start_time, '%Y/%m/%d' ) AS startTime from course  GROUP BY startTime  ORDER BY startTime limit 6  ");
        return  this.querySqlObjects(hql.toString());
    }
}